﻿/******************************************************************************
 *
 * @copyright:          Copyright 2011 UniSky Ltd.
 * @file:
 * @author:              kenping.liu
 * @creation:
 * @description:
 *
 * @modifications
 * date         user                description
 *
 *
 * @more-details
 *
 *****************************************************************************/

#include "u_inc.h"

U_NAMESPACE_BEGIN(u)

TCHAR* get_error_msg(long err_code)
{
    static TCHAR tmp[10240] = {0};
    ZeroMemory(tmp, 10240);

#if defined(WINDOWS)
    if ( 0==err_code )
    {
        err_code = GetLastError();
    }
    LPTSTR buf = NULL;
    if ( ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL, err_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR)&buf, 0, NULL) )
    {
        // DWORD language_id
        // MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
        // MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)
        // MAKELANGID(LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED)
        // MAKELANGID(LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL)

        u_sprintf(tmp, _T(" [%d]%s"), err_code, buf);
        ::LocalFree(buf);
    }
    else
    {
        u_sprintf(tmp, _T(" [%d]"), err_code);
    }
#else
    u_sprintf(tmp, _T(" [%d]"), (int)err_code);
#endif
    return (tmp);
}

U_NAMESPACE_END(u)
